<html>
<head><meta charset="utf-8"><title>Constrained Type Parameter Definition · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html">Constrained Type Parameter Definition</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245283245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283245">(Jul 08 2021 at 08:01)</a>:</h4>
<p>Does anybody have a definition for when a type parameter is considered constrained?</p>



<a name="245283357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283357">(Jul 08 2021 at 08:02)</a>:</h4>
<p>Or explain to me, why in the following code V is not considered constrained:</p>
<div class="codehilite"><pre><span></span><code>impl&lt;T,U,V&gt; Foo&lt;T&gt; for T
    where (T,U): Bar&lt;Out=V&gt;
</code></pre></div>



<a name="245283689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283689">(Jul 08 2021 at 08:06)</a>:</h4>
<p>might be becuase <code>U</code> isnt constrained?</p>



<a name="245283827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283827">(Jul 08 2021 at 08:08)</a>:</h4>
<p>No, here <code>U</code> is also unconstrained:</p>
<div class="codehilite"><pre><span></span><code>impl&lt;T,U&gt; Bar for T
    where T : Bar&lt;Out=U&gt;
</code></pre></div>



<a name="245283894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283894">(Jul 08 2021 at 08:09)</a>:</h4>
<p>I suppose a type parameter has to occur as an input parameter for it to be constrained, but would be nice if somebody could validate that.</p>



<a name="245283910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283910">(Jul 08 2021 at 08:09)</a>:</h4>
<p>I could have sworn that should consider <code>U</code> to be constrained</p>



<a name="245283923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245283923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245283923">(Jul 08 2021 at 08:09)</a>:</h4>
<p>yeah so did i</p>



<a name="245284000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284000">(Jul 08 2021 at 08:10)</a>:</h4>
<p>i thought the rules were that the param has to be used in the self type or trait or used as an assoc type bound..? but that 3rd one seems to not be the case</p>



<a name="245284273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284273">(Jul 08 2021 at 08:13)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span>: <span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="245284285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284285">(Jul 08 2021 at 08:13)</a>:</h4>
<p>this compiles</p>



<a name="245284465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284465">(Jul 08 2021 at 08:15)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">type</span> <span class="nc">Assoc</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span>: <span class="nc">Bar</span><span class="o">&lt;</span><span class="n">Assoc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Assoc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">u32</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>this seems to not</p>



<a name="245284537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284537">(Jul 08 2021 at 08:16)</a>:</h4>
<p>hm interesting. kind of confusing without a proper definition.</p>



<a name="245284560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284560">(Jul 08 2021 at 08:16)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">type</span> <span class="nc">Assoc</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">trait</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">U</span>: <span class="nc">Bar</span><span class="o">&lt;</span><span class="n">Assoc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">Trait</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>oh this on the otherhand <em>does</em> compile</p>



<a name="245284749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284749">(Jul 08 2021 at 08:19)</a>:</h4>
<p>this is implemented <a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/constrained_generic_params.rs">here</a> in the compiler</p>



<a name="245284811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284811">(Jul 08 2021 at 08:20)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/constrained_generic_params.rs#L188-L194">https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/constrained_generic_params.rs#L188-L194</a></p>



<a name="245284820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284820">(Jul 08 2021 at 08:20)</a>:</h4>
<p>might be this? no idea</p>



<a name="245284842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245284842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245284842">(Jul 08 2021 at 08:20)</a>:</h4>
<p>the not-compiling code does infact seem to be using an assoc type from the trait we're implementing</p>



<a name="245285400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245285400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245285400">(Jul 08 2021 at 08:27)</a>:</h4>
<p>yes I think you're right, that seems to be the reason why your example doesn't compile.</p>



<a name="245285484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245285484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245285484">(Jul 08 2021 at 08:28)</a>:</h4>
<p>This doesn't compile either though:</p>
<div class="codehilite"><pre><span></span><code>struct S&lt;F&gt;(F);

impl &lt;&#39;a, F, G&gt; S&lt;F&gt;
where
    F: Fn(&amp;&#39;a u8) -&gt; G
{
    fn call(&amp;mut self) -&gt; G {
        (self.0)(&amp;1)
    }
}
</code></pre></div>
<p>and this seems to be for different reasons, here G is considered unconstrained.</p>



<a name="245285705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245285705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BN <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245285705">(Jul 08 2021 at 08:30)</a>:</h4>
<p>There we hit <a href="https://github.com/rust-lang/rust/blob/ac8c3bfffb2829d0af075a5d4020fb7e9e86c3a2/compiler/rustc_typeck/src/constrained_generic_params.rs#L201-L205">this conditional</a> and never process the type for the projection. But simply moving the code that processes the type for projection before that if statement causes code like my first example not to throw an error on <code>V</code>, which probably is not correct.</p>



<a name="245455019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Constrained%20Type%20Parameter%20Definition/near/245455019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition.html#245455019">(Jul 09 2021 at 15:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="328097">BN</span> <a href="#narrow/stream/131828-t-compiler/topic/Constrained.20Type.20Parameter.20Definition/near/245285484">said</a>:</p>
<blockquote>
<p>This doesn't compile either though:</p>
<div class="codehilite"><pre><span></span><code>struct S&lt;F&gt;(F);

impl &lt;&#39;a, F, G&gt; S&lt;F&gt;
where
    F: Fn(&amp;&#39;a u8) -&gt; G
{
    fn call(&amp;mut self) -&gt; G {
        (self.0)(&amp;1)
    }
}
</code></pre></div>
<p>and this seems to be for different reasons, here G is considered unconstrained.</p>
</blockquote>
<p><code>'a</code> is unconstrained, there, and we could have <code>G = &amp;'a u8</code>, for instance. Either you meant <code>'a = 'static</code>, or <code>F : for&lt;'a&gt; Fn(&amp;'a …</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>